/*
 * @(#)MessagesTag.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.framework.taglib.html;

import java.util.Iterator;
import java.util.Locale;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.util.MessageResources;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;

/**
  MessagesTag.java

  <p>This class is to extend the MessagesTag provided by Apache Struts.</p>
  <p>Modification in "struts-html.tld"<br>
  <pre>
     [FROM]
     &lt;tagclass&gt;org.apache.struts.taglib.html.MessagesTag&lt;/tagclass&gt;
     &lt;teiclass&gt;org.apache.struts.taglib.html.MessagesTei&lt;/teiclass&gt;
     &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
     &lt;attribute&gt;
     &lt;name&gt;id&lt;/name&gt;
     &lt;required&gt;true&lt;/required&gt;
     &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;

     [TO]
     &lt;tagclass&gt;com.dcivision.framework.taglib.html.MessagesTag&lt;/tagclass&gt;
     &lt;bodycontent&gt;empty&lt;/bodycontent&gt;
     &lt;attribute&gt;
     &lt;name&gt;id&lt;/name&gt;
     &lt;required&gt;false&lt;/required&gt;
     &lt;rtexprvalue&gt;true&lt;/rtexprvalue&gt;
     &lt;/attribute&gt;
  </pre>

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   12/07/2003
    @version         $Revision: 1.8 $
*/

public class MessagesTag extends TagSupport {

  public static final String REVISION = "$Revision: 1.8 $";

  /**
   * The message resources for this package.
   */
  protected static MessageResources messages = MessageResources.getMessageResources(Constants.Package + ".LocalStrings");
  protected static Locale defaultLocale = Locale.getDefault();
  protected String bundle = null;
  protected String locale = org.apache.struts.Globals.LOCALE_KEY;
  protected String name = org.apache.struts.Globals.MESSAGE_KEY;
  /**
   * The name of the property for which message messages should be returned,
   * or <code>null</code> to return all messages.
   */
  protected String property = null;


  public String getBundle() {
    return (this.bundle);
  }

  public void setBundle(String bundle) {
    this.bundle = bundle;
  }

  public String getLocale() {
    return (this.locale);
  }

  public void setLocale(String locale) {
    this.locale = locale;
  }

  public String getName() {
    return (this.name);
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getProperty() {
    return (this.property);
  }

  public void setProperty(String property) {
    this.property = property;
  }

  /**
   * Render the specified message messages if there are any.
   *
   * @exception JspException if a JSP exception has occurred
   */
  public int doStartTag() throws JspException {

    // Were any message messages specified?
    ActionMessages messages = null;
    try {
      messages = RequestUtils.getActionMessages(pageContext, name);
    } catch (JspException e) {
      RequestUtils.saveException(pageContext, e);
      throw e;
    }
    if ((messages == null) || messages.isEmpty()) {
      return (EVAL_BODY_INCLUDE);
    }

    // Check for presence of header and footer message keys
    boolean headerPresent =
      RequestUtils.present(pageContext, bundle, locale, "common.message.prefix");
    boolean footerPresent =
      RequestUtils.present(pageContext, bundle, locale, "common.message.suffix");

    // Render the message messages appropriately
    StringBuffer results = new StringBuffer();
    boolean headerDone = false;
    String message = null;
    Iterator reports = null;

    results.append(RequestUtils.message(pageContext, bundle, locale, "common.message.header"));

    if (property == null) {
      reports = messages.get();
    } else {
      reports = messages.get(property);
    }
    while (reports.hasNext()) {
      ActionMessage report = (ActionMessage) reports.next();
      if (!headerDone) {
        if (headerPresent) {
          message = RequestUtils.message(pageContext, bundle,
                           locale, "common.message.prefix");
          results.append(message);
          results.append("\r\n");
        }
        headerDone = true;
      }
      if (report.getKey() != null && report.getKey().startsWith("customize")) {
        message = RequestUtils.message(pageContext, "customize",
                                       locale, report.getKey(),
                                       report.getValues());
      } else {
        message = RequestUtils.message(pageContext, bundle,
                                       locale, report.getKey(),
                                       report.getValues());
      }
      if (message != null) {
        results.append(message);
        results.append("\r\n<br>");
      }
    }
    if (headerDone && footerPresent) {
      message = RequestUtils.message(pageContext, bundle,
                       locale, "common.message.suffix");
      results.append(message);
      results.append("\r\n");
    }

    results.append(RequestUtils.message(pageContext, bundle, locale, "common.message.footer"));

    // Print the results to our output writer
    ResponseUtils.write(pageContext, results.toString());

    // Continue processing this page
    return (EVAL_BODY_INCLUDE);
  }

  /**
   * release
   *
   * Release any acquired resources.
   */
  public void release() {
    super.release();
    bundle = org.apache.struts.Globals.MESSAGES_KEY;
    locale = org.apache.struts.Globals.LOCALE_KEY;
    name = org.apache.struts.Globals.MESSAGE_KEY;
    property = null;
  }

}
